package cn.ynu.cmrs.asyncTask;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;

import cn.ynu.cmrs.R;
import cn.ynu.cmrs.MyApplication;
import cn.ynu.cmrs.activity.FavoriteActivity;
import cn.ynu.cmrs.utils.JsonParse;
import cn.ynu.cmrs.utils.WebInterface;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;

/**
 * AsyncTask, Remove one course from favorite
 * 
 * @author Yasha
 *
 */
public class RemoveFavoriteTask extends AsyncTask<String, Void, String[]> {

	protected Context mContext;
	protected int mSuccessMsg;
	protected int mFailMsg;
	protected String mCID;

	public RemoveFavoriteTask(Context mContext, int mSuccessMsg, int mFailMsg) {
		super();
		this.mContext = mContext;
		this.mSuccessMsg = mSuccessMsg;
		this.mFailMsg = mFailMsg;
	}

	@Override
	protected String[] doInBackground(String... params) {
		// TODO Auto-generated method stub
		mCID = params[0];
		Map<String, String> param = new HashMap<String, String>();
		param.clear();
		param.put("interface", "delcollect");
		param.put("userid", MyApplication.nowUser.getId());
		param.put("courseid", mCID);
		String result = WebInterface.webInterface(param);
		String[] status;
		try {
			status = JsonParse.getStatus(result);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			status = new String[] { "8086",
					mContext.getString(R.string.inner_error_json) };
		}
		return status;
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}

	@Override
	protected void onPostExecute(String[] result) {
		// TODO Auto-generated method stub
		if (result[0].equals("0")) {
			Toast.makeText(mContext, mCID + mContext.getString(mSuccessMsg), Toast.LENGTH_SHORT)
					.show(); 
			//FavoriteActivity.setRefreshFlag(true);
			FavoriteActivity.sendRefreshMsg();
		} else {
			Toast.makeText(mContext,
					mCID + mContext.getString(mFailMsg) + result[1], Toast.LENGTH_SHORT).show();
		}
		super.onPostExecute(result);
	}

}
